﻿using K8.CBB;
using System.IO;
using System.Linq;

namespace KingerRobot.UnitTest;
[TestClass]
public sealed class Program
{
    [AssemblyInitialize()]
    public static void AssemblyInit(TestContext context)
    {
        ini_main();
    }

    static void ini_main()
    {
        void ini_db_config_sql()
        {
            var dbConfig = new DbConfig();
            dbConfig.DbAlias = "MsSql";
            dbConfig.DbType = "MsSql";
            dbConfig.DbHost = "10.22.4.26";
            dbConfig.DbUser = "sa";
            dbConfig.DbPwd = "WE19gDFvLK_7";
            dbConfig.DbName = "BWPDB";
            XApp.Current.DbConfig = dbConfig.ToJson();
        }
        //void ini_db_config_sql()
        //{
        //    var dbConfig = new DbConfig();
        //    dbConfig.DbAlias = "MsSql";
        //    dbConfig.DbType = "MsSql";
        //    dbConfig.DbHost = "127.0.0.1";
        //    dbConfig.DbUser = "sa";
        //    dbConfig.DbPwd = "123456";
        //    dbConfig.DbName = "BWPDB";
        //    XApp.Current.DbConfig = dbConfig.ToJson();
        //}
        //void ini_db_config_sql()
        //{
        //    var dbConfig = new DbConfig();
        //    dbConfig.DbAlias = "Oracle";
        //    dbConfig.DbType = "Oracle";
        //    dbConfig.DbHost = "172.17.188.193";
        //    dbConfig.DbPort = 1521;
        //    dbConfig.DbUser = "KJ";
        //    dbConfig.DbPwd = "password";
        //    dbConfig.DbName = @"{""ServiceName"":""orcl.localdomain"",""DbSchema"":""KJ"",""DbVersion"":""11""}";
        //    XApp.Current.DbConfig = dbConfig.ToJson();
        //}

        //void ini_db_config_sql()
        //{
        //    var dbConfig = new DbConfig();
        //    dbConfig.DbAlias = "Oracle";
        //    dbConfig.DbType = "Oracle";
        //    dbConfig.DbHost = "localhost";
        //    dbConfig.DbPort = 1521;
        //    dbConfig.DbUser = "KJ";
        //    dbConfig.DbPwd = "password";
        //    dbConfig.DbName = @"{""ServiceName"":""ORCL"",""DbSchema"":""KJ"",""DbVersion"":""11""}";
        //    XApp.Current.DbConfig = dbConfig.ToJson();
        //}
        void ini_db_config_redis()
        {
            var dbConfig = new DbConfig();
            dbConfig.DbAlias = "K8.Redis";
            dbConfig.DbType = "Redis";
            dbConfig.DbHost = "127.0.0.1";
            dbConfig.DbPort = 6379;
            dbConfig.DbUser = string.Empty;
            dbConfig.DbPwd = "OGsFD3QLh5LT";
            dbConfig.DbName = "0";
            XApp.Current.RedisDB = dbConfig.ToJson();
        }
        void ini_run_app_main()
        {
            try
            {
                // 指定要搜索的根目录
                string rootDirectory = @"D:\K8";

                // 获取所有以“x-runtimes”开头的文件夹
                var runtimeDirectories = Directory.GetDirectories(rootDirectory, "x-runtimes*", SearchOption.AllDirectories);

                foreach (var directory in runtimeDirectories)
                {
                    // 获取目录中的所有 DLL 文件
                    var dllFiles = Directory.GetFiles(directory, "*.dll", SearchOption.AllDirectories);

                    foreach (var dll in dllFiles)
                    {
                        try
                        {
                            // 加载 DLL 程序集
                            Assembly.LoadFrom(dll);
                            //Console.WriteLine($"Successfully loaded: {dll}");
                        }
                        catch (Exception ex)
                        {
                           // Console.WriteLine($"Failed to load {dll}: {ex.Message}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        //void ini_run_app_main()
        //{
        //    try
        //    {
        //        var log_action = new Action<string>((msg) =>
        //        {
        //            return;
        //        });
        //        var typeName = $"K8.App.Program,K8.App";
        //        var type = Type.GetType(typeName, false, true);
        //        var method = type.GetMethod("Main", TypeExtend.DefaultBindingFlags(), [typeof(string[])]);
        //        if (method != null)
        //        {
        //            method.Invoke(null, new object[method.GetParameters().Length]);
        //            return;
        //        }
        //    }
        //    catch (Exception)
        //    {

        //    }
        //}

        void ini_db_config()
        {
            ini_db_config_sql();
            ini_db_config_redis();
        }
        ini_run_app_main();
        ini_db_config();
    }
}
